package framework.util.http;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;

/**
 * java调用HTTP的工具类
 * 
 * @blog https://www.cnblogs.com/hhhshct/p/8523697.html
 * @author 吴宇亮 2019年4月2日下午5:32:51
 */
public class _HttpUtil {

    /**
     * 获取用户真实的ip地址
     *
     * @author 吴宇亮 on 2019/6/13 0013 上午 11:20
     */
    public static String getIpAdrress(HttpServletRequest request) {
        String Xip = request.getHeader("X-Real-IP");
        String XFor = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = XFor.indexOf(",");
            if(index != -1){
                return XFor.substring(0,index);
            }else{
                return XFor;
            }
        }
        XFor = Xip;
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            return XFor;
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getRemoteAddr();
        }
        return XFor;
    }
   
   /**
    * 发起get请求
    * @param url 请求路径
    *
    * @author 吴宇亮 2019年4月2日下午5:46:05
    */
   public static String doGet(String url) throws ParseException, IOException{
      CloseableHttpClient httpClient = HttpClients.createDefault();
      HttpGet httpGet = new HttpGet(url);
      CloseableHttpResponse _response = httpClient.execute(httpGet);
      
      try {
          HttpEntity entity = _response.getEntity();
          String response = EntityUtils.toString(entity, "utf-8");
          EntityUtils.consume(entity);
          return response;
      } finally {
         _response.close();
      }
   }

    @Test
    public void doPostTest() throws IOException {
        String url = "https://www.mxnzp.com/api/barcode/goods/details?barcode=6936909900285";
        String response = doGet(url);
        System.out.println(response);
    }
   
   /**
    * @param contentType 报文类型，可以为：application/json或application/x-www-form-urlencoded等
    * @param url 请求路径
    * @param requsetParam 请求参数
    * @param charset 请求参数的编码
    * 
    * @author 吴宇亮 2019年4月2日下午5:46:39
    */
   public static String doPost(String contentType, String url, String requsetParam, String charset) throws IOException {
       CloseableHttpClient httpclient = HttpClients.createDefault();
       HttpPost httpPost = new HttpPost(url);
       httpPost.setHeader("Content-Type", contentType);
       httpPost.setEntity(new StringEntity(requsetParam, charset));
       CloseableHttpResponse _response = httpclient.execute(httpPost);
       
       try {
           HttpEntity entity = _response.getEntity();
           String response = EntityUtils.toString(entity, charset);
           EntityUtils.consume(entity);
           return response;
       } finally {
           _response.close();
       }
   }



}


























